package de.schildbach.wallet.menu;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.ReceiverCallNotAllowedException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.mypgwali.R;
import de.schildbach.wallet.ui.ExchangeRatesFragment;
import de.schildbach.wallet.ui.PeerListFragment;
import de.schildbach.wallet.ui.RequestCoinsFragment;
import de.schildbach.wallet.ui.SendCoinsFragment;
import de.schildbach.wallet.ui.TransactionsListFragment;
import de.schildbach.wallet.ui.WalletBalanceFragment;

public class MenuFragment extends ListFragment {

	public static final int DOP_MENU = 2;

	private SampleAdapter adapter;

	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		ListView list = (ListView) inflater.inflate(R.layout.menu_list, null);

		View header = (View) inflater.inflate(R.layout.menu_header, null);

		list.addHeaderView(header);
		

		


		return list;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		adapter = new SampleAdapter(getActivity());

		setListAdapter(adapter);
	}

	private class MenuItem {
		public String tag;
		public int iconRes;
		public String locale;

		public MenuItem(String tag, int iconRes) {
			this.tag = tag;
			this.iconRes = iconRes;
		}

	}

	public void notifyDateChanged() {
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onListItemClick(ListView lv, View v, int p, long id) {
		Fragment newContent = null;
		String title = "";

		if ( p == 0) {
			return;
		}
		if (p == 1) {
			newContent = new WalletBalanceFragment();
			
		/////} else if (p == 2) {
		//	newContent = new SendCoinsFragment();
		//} else if (p == 3) {
		//	newContent = new RequestCoinsFragment();
		} else if (p == 2) {
			newContent = new TransactionsListFragment();
		} else if (p == 3) {
			newContent = new ExchangeRatesFragment();
		} else if (p == 4) {
			newContent = new PeerListFragment();
		} 
		
		
		title = titleMap.get(p - 1);
		if (title == null) {
			title = "";
		}
		if (newContent != null) {
			de.schildbach.wallet.menu.BaseActivity bA = (BaseActivity) getActivity();
			boolean wallet = p == 1;
			bA.switchContent(newContent, title, wallet);
		}

	}
	static Map<Integer, String> titleMap = new HashMap<Integer, String>();
	
	static Map<Integer, Integer> icons = new HashMap<Integer, Integer>();
	static {
		
		titleMap.put(0, "Balance");
	//	titleMap.put(1, "Send");
	//	titleMap.put(2, "Request");
		titleMap.put(1, "Transactions");
		titleMap.put(2, "Exchange Rates");
		titleMap.put(3, "Peers");

		
		
		icons.put(0, R.drawable.balance);
	///	icons.put(1, R.drawable.send);
	//	icons.put(2, R.drawable.request);
		icons.put(1, R.drawable.history);
		icons.put(2, R.drawable.exchange);
		icons.put(3, R.drawable.peers);
	}
	

	public class SampleAdapter extends ArrayAdapter<MenuItem> {

		
		public SampleAdapter(Context context) {
			super(context, 0);

		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.row, null);
			}

			MenuItem i = getItem(position);

			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			TextView text = (TextView) convertView.findViewById(R.id.row_title);
			icon.setImageResource(i.iconRes);
			text.setText(i.tag);
			
			return convertView;
		}

		@Override
		public int getCount() {
			return 4;
		}

		@Override
		public MenuItem getItem(int p) {
			return new MenuItem(titleMap.get(p), icons.get(p));

		}

	}

}
